package com.jai.pms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.*;
import com.jai.pms.mapper.ProjectDataMapper;
import com.jai.pms.mapper.ProjectMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.ShotMapper;
import com.jai.pms.service.IShotService;

/**
 * 镜头管理Service业务层处理
 *
 * @author v
 * @date 2023-09-19
 */
@Service
public class ShotServiceImpl extends BaseDataImpl implements IShotService {
    @Autowired
    private ShotMapper shotMapper;

    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询镜头管理
     *
     * @param id 镜头管理主键
     * @return 镜头管理
     */
    @Override
    public Shot selectShotById(Long id) {
        return shotMapper.selectShotById(id);
    }

    /**
     * 查询镜头管理列表
     *
     * @param shot 镜头管理
     * @return 镜头管理
     */
    @Override
    public List<Shot> selectShotList(Shot shot) {
        return shotMapper.selectShotList(shot);
    }

    /**
     * 新增镜头管理
     *
     * @param shot 镜头管理
     * @return 结果
     */
    @Override
    public int insertShot(Shot shot) {
        preCheck(shot, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        shot.setCreateBy(loginUser.getUsername());
        shot.setCreateTime(DateUtils.getNowDate());
        return shotMapper.insertShot(shot);
    }

    /**
     * 修改镜头管理
     *
     * @param shot 镜头管理
     * @return 结果
     */
    @Override
    public int updateShot(Shot shot) {
        preCheck(shot, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        shot.setUpdateBy(loginUser.getUsername());
        shot.setUpdateTime(DateUtils.getNowDate());
        return shotMapper.updateShot(shot);
    }

    /**
     * 批量删除镜头管理
     *
     * @param ids 需要删除的镜头管理主键
     * @return 结果
     */
    @Override
    public int deleteShotByIds(Long[] ids) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("ids", ids);
        params.put("updateBy", SecurityUtils.getLoginUser().getUsername());
        params.put("updateTime", DateUtils.getNowDate());
        return shotMapper.deleteShotByIds(params);
    }

    /**
     * 删除镜头管理信息
     *
     * @param id 镜头管理主键
     * @return 结果
     */
    @Override
    public int deleteShotById(Long id) {
        return shotMapper.deleteShotById(id);
    }

    private Shot preCheck(Shot shot, boolean isAdd) {
        // 检查是否有重复信息，联合主键projectId和code
        Shot query = new Shot();
        query.setProjectId(shot.getProjectId());
        query.setCode(shot.getCode());
        List<Shot> queryList = shotMapper.selectShotList(query);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该项目中已经存在此编码 !");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), shot.getId())) {
                throw new ServiceException("该项目中已经存在此编码 !");
            }
        }

        // 填充父类详细信息
        BaseDataEntity parent = getParent(shot.getProjectId(), "shot", shot.getParentCode());
        shot.setParentId(parent.getId());
        shot.setParentCode(parent.getCode());
        shot.setParentName(parent.getName());
        shot.setAncestors(parent.getAncestors());

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(shot.getProjectId());
        shot.setProjectCode(project.getCode());
        shot.setProjectName(project.getName());

        return shot;
    }
}
